package org.uim.cricket.updater.parser.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.uim.cricket.updater.Ulogger;
import org.uim.cricket.updater.entities.CricketMatchNew;
import org.uim.cricket.updater.parser.MatchListParsable;
import org.uim.cricket.util.HTMLStringUtils;
import org.uim.cricket.util.MyStringUtils;

public class DefaultCricketMatchParser implements MatchListParsable {

	public static final String START_POSITION_TEXT = "<!-- START 650 AREA -->";
	
	@Override
	public List<CricketMatchNew> parseNewMatches(StringBuffer sb) throws Exception {
		List<CricketMatchNew> matches = new ArrayList<CricketMatchNew>();
		
		int pos = sb.indexOf(START_POSITION_TEXT);
		if (pos < 0) {
			Ulogger.debug("  not found start point of match list! exiting.");
			return matches;
		}
		
		String src = sb.substring(pos + START_POSITION_TEXT.length());
		
		String table = MyStringUtils.inBetween(src, "<table", "</table>");
		if (table == null) {
			Ulogger.debug("  not found match list table! exiting.");
			return matches;
		}
		table = "<table" + table + "</table>";
		
		String tbody = MyStringUtils.inBetween(table, "<tbody>", "</tbody>");
		if (tbody == null) {
			Ulogger.debug("  not found match table body! exiting.");
			return matches;
		}
		tbody = "<tbody>" + tbody + "</tbody>";
		
		String[] trs = MyStringUtils.splitInBetween(tbody, "<tr", "</tr>");
		if (trs == null || trs.length == 0) {
			Ulogger.debug(" no match rows are found! exiting.");
			return matches;
		}
		
		for (int r=0;r<trs.length;r++) {
			String row = "<tr" + trs[r] + "</tr>";
			
			String[] tds = MyStringUtils.splitInBetween(row, "<td", "</td>");
			if (tds == null || tds.length == 0) continue;
			
			
			if (tds.length == 7) {
				CricketMatchNew match = new CricketMatchNew();
				
				match.setTeam1(HTMLStringUtils.cleanHTMLTags("<td" + tds[0]));
				match.setTeam2(HTMLStringUtils.cleanHTMLTags("<td" + tds[1]));
				String teamWon = HTMLStringUtils.cleanHTMLTags("<td" + tds[2]);
				if (teamWon.startsWith("drawn")) {
					match.setWinTeam("");
				} else {
					match.setWinTeam(teamWon);
				}
				match.setResult(HTMLStringUtils.cleanHTMLTags("<td" + tds[3]));
				
				String ground = MyStringUtils.inBetween("<td" + tds[4], "<a", "</a>");
				if (ground != null) {
					HashMap<String, String> attribs = HTMLStringUtils.getHTMLAttributes("<a" + ground);
					if (attribs != null && attribs.containsKey("href")) {
						match.setGroundURL(attribs.get("href"));
					}
					match.setGround(HTMLStringUtils.cleanHTMLTags("<a" + ground));
				}
				
				match.setDate(HTMLStringUtils.cleanHTMLTags("<td" + tds[5]));
				
				String mid = MyStringUtils.inBetween("<td" + tds[6], "<a", "</a>");
				if (mid != null) {
					HashMap<String, String> attribs = HTMLStringUtils.getHTMLAttributes("<a" + mid);
					if (attribs != null && attribs.containsKey("href")) {
						match.setMatchURL(attribs.get("href"));
					}
					match.setMatchId(HTMLStringUtils.cleanHTMLTags("<a" + mid));
				}
				
				matches.add(match);
			}
			
		}
		
		return matches;
	}

}
